package com.caval.controller;

import com.alibaba.fastjson.JSON;
import com.caval.service.CartService;
import com.caval.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class AccountController {

    @Autowired
    @Qualifier("cartServiceImpl")
    private CartService cartService;

    @Autowired
    @Qualifier("warehouseServiceImpl")
    private WarehouseService warehouseService;

    public void setCartService(CartService cartService) {
        this.cartService = cartService;
    }

    public void setWarehouseService(WarehouseService warehouseService) {
        this.warehouseService = warehouseService;
    }

    //获取购物车信息
    @RequestMapping("/cart/getShoppingCart")
    @ResponseBody
    public String getCartList(HttpSession session){
        return JSON.toJSONString(cartService.getCartList((int) session.getAttribute("user_id")));
    }

    //添加进购物车
    @RequestMapping("/cart/addtoCart")
    @ResponseBody
    public String addtoCart(int game_id, HttpSession session){
        return cartService.addtoCart((int) session.getAttribute("user_id"),game_id);
    }

    //获取游戏仓库
    @RequestMapping("/warehouse/getWarehouse")
    @ResponseBody
    public String getWarehouse(HttpSession session){
        return JSON.toJSONString(warehouseService.getWarehouse((int) session.getAttribute("user_id")));
    }

    //购买游戏
    @RequestMapping("/warehouse/doPurchase")
    @ResponseBody
    public String doPurchase(int game_id, HttpSession session){
        return warehouseService.doPurchase((int) session.getAttribute("user_id"),game_id);
    }

    //是否已经在仓库内
    @RequestMapping("/warehouse/ifinWarehouse")
    @ResponseBody
    public String ifinWarehouse(int game_id, HttpSession session){
        return warehouseService.gameExist((int) session.getAttribute("user_id"), game_id);
    }

    //退款
    @RequestMapping("/warehouse/refund")
    @ResponseBody
    public String refund(int game_id, HttpSession session){
        return warehouseService.doRefund((int) session.getAttribute("user_id"),game_id);
    }

    //移出购物车
    @RequestMapping("/cart/outofCart")
    @ResponseBody
    public String outofCart(int game_id, HttpSession session){
        return cartService.outofCart((int) session.getAttribute("user_id"),game_id);
    }

    @RequestMapping("/recharge/doRecharge")
    @ResponseBody
    public String doRecharge(int amount, HttpSession session){
        return warehouseService.doRecharge((int) session.getAttribute("user_id"),amount);
    }
}
